package www.lagou.app;

import www.lagou.dao.AccountDao;
import www.lagou.dao.TransactionDao;
import www.lagou.entity.Account;
import www.lagou.entity.Transaction;
import www.lagou.utils.DateUtils;
import www.lagou.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 编程题1
 */
public class Work01 {

    // 实现卡号：1122334455向55443332211转账5000元的操作；
    public static void main(String[] args) {

        Connection connection = DruidUtils.getConnection();

        // 转账金额
        double trasactionMoney = 5000;
        String a1Card = "1122334455";
        String a2Card = "55443332211";

        try {
            // 开启事务
            connection.setAutoCommit(false);

            AccountDao accountDao = new AccountDao();
            TransactionDao transactionDao = new TransactionDao();

            Account account1 = accountDao.findAccountByCard(connection,a1Card);
            Account account2 = accountDao.findAccountByCard(connection,a2Card);


            if (account1 == null) {
                System.out.println("卡号：" + a1Card + "  不存在");
                return;
            }

            if (account2 == null) {
                System.out.println("卡号：" + a2Card + "  不存在");
                return;
            }

            if (account1.getBalance() < trasactionMoney) {
                System.out.println("卡号：" + a1Card + "  余额不足！");
                return;
            }

            // 更新 account1 的数据
            account1.setBalance(account1.getBalance() - trasactionMoney);
            // 更新 account2 的数据
            account2.setBalance(account2.getBalance() + trasactionMoney);



            // 更新 account1 在 account表 中 的数据
            accountDao.updateAccountBalance(connection,account1);
            // 更新 account2 在 account表 中 的数据
            accountDao.updateAccountBalance(connection,account2);


            // TODO ： 模拟异常
//            System.out.println(1 / 0);

            // 创建 transaction 转出和转入对象，并且赋值
            Transaction transaction1 = new Transaction();
            transaction1.setCardid(account1.getCard());
            transaction1.setTramoney(trasactionMoney);
            transaction1.setTratype("转出");
            transaction1.setTradate(DateUtils.getDateFormart());

            Transaction transaction2 = new Transaction();
            transaction2.setCardid(account2.getCard());
            transaction2.setTramoney(trasactionMoney);
            transaction2.setTratype("转入");
            transaction2.setTradate(DateUtils.getDateFormart());

            // 插入 transaction 转入 和转出数据
            transactionDao.insert(connection,transaction1);
            transactionDao.insert(connection,transaction2);

            // 提交事务
            connection.commit();
        } catch (SQLException e) {

            e.printStackTrace();

            try {
                // 出现异常 ，回滚事务
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


    }
}
